﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.ListDependantCardsRequest
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Microsoft.InfoCards
{
  internal class ListDependantCardsRequest : UIAgentRequest
  {
    private List<InfoCard> m_allCards = new List<InfoCard>();
    private Uri m_selfIssuedCardId;

    public ListDependantCardsRequest(
      IntPtr rpcHandle,
      Stream inArgs,
      Stream outArgs,
      ClientUIRequest parent)
      : base(rpcHandle, inArgs, outArgs, parent)
    {
    }

    protected override void OnMarshalInArgs()
    {
      this.m_selfIssuedCardId = new Uri(Utility.DeserializeString((BinaryReader) new InfoCardBinaryReader(this.InArgs, Encoding.Unicode)));
    }

    protected override void OnProcess()
    {
      StoreConnection connection = StoreConnection.GetConnection();
      try
      {
        IList<DataRow> dataRowList = (IList<DataRow>) connection.Query(QueryDetails.FullRow, connection.LocalDataSource, new QueryParameter("ix_supportauth", new object[1]
        {
          (object) 2
        }));
        if (dataRowList == null || dataRowList.Count == 0)
          return;
        for (int index = 0; index < dataRowList.Count; ++index)
        {
          byte[] dataField = dataRowList[index].GetDataField();
          try
          {
            using (MemoryStream memoryStream = new MemoryStream(dataField))
            {
              InfoCard infoCard = new InfoCard((Stream) memoryStream);
              foreach (TokenCreationParameter creationParameter in infoCard.CreationParameters)
              {
                if (creationParameter.CredentialType == TokenFactoryCredentialType.SelfIssuedCredential && Utility.CompareByteArrays(Convert.FromBase64String(creationParameter.CredentialSelectors[CredentialSelectorType.SelfIssuedCardIdSelector].GetStringWithoutNullTerminator()), Utility.CreateHash(infoCard.IssuerIdentifierAsBytes, this.m_selfIssuedCardId)))
                  this.m_allCards.Add(infoCard);
              }
            }
          }
          finally
          {
            Array.Clear((Array) dataField, 0, dataField.Length);
          }
        }
      }
      finally
      {
        connection.Close();
      }
    }

    protected override void OnMarshalOutArgs()
    {
      BinaryWriter writer = new BinaryWriter(this.OutArgs, Encoding.Unicode);
      if (this.m_allCards != null)
      {
        writer.Write(this.m_allCards.Count);
        foreach (InfoCard allCard in this.m_allCards)
          Utility.SerializeUri(writer, allCard.Id);
      }
      else
        writer.Write(0);
    }
  }
}
